import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gameplay_flutter/m_room/constants/room_assets.dart';
import 'package:gameplay_flutter/m_room/room_manager.dart';
import 'package:get/get.dart';

import 'room_main_controller.dart';
import 'room_page/room_page.dart';

class RoomMainPage extends StatefulWidget {
  const RoomMainPage({Key? key}) : super(key: key);

  @override
  _RoomMainPageState createState() => _RoomMainPageState();
}

class _RoomMainPageState extends State<RoomMainPage> {
  RoomMainController controller = Get.put(RoomMainController());
  late PageController _pageController;

  @override
  void initState() {
    super.initState();
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.light,
      statusBarBrightness: Brightness.dark,
      systemNavigationBarColor: Colors.transparent,
    ));
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    _pageController = PageController(initialPage: controller.currentIndex.value);
    RoomManager.instance.initSdk().then((v){
      if (mounted) {
        onPageChanged(controller.currentIndex.value);
      }
    });
  }

  void onPageChanged(int index) {
    controller.currentIndex.value = index;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage(RoomAssets.roomBg),
            fit: BoxFit.cover,
          ),
        ),
        child: Obx(() => PageView.builder(
              itemCount: controller.roomDatas.value.length,
              controller: _pageController,
              scrollDirection: Axis.vertical,
              onPageChanged: onPageChanged,
              itemBuilder: (context, index) {
                return RoomPage(index: index);
              },
            )));
  }

  @override
  void dispose() {
    _pageController.dispose();
    RoomManager.instance.dispose();
    super.dispose();
  }
}
